package com.web.xbq;

import com.entity.CatPost;
import com.entity.PageManager;
import com.entity.User;
import com.service.xbq.HeadService;
import com.service.xbq.RelInformationService;
import com.utils.EncryptCodeUtils;
import com.utils.WebUtils;
import com.web.lqj.UserService_lqj;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@WebServlet("/rellInformation")
@MultipartConfig
public class RelInformationServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String, String[]> parameterMap = request.getParameterMap();
        RelInformationService service = new RelInformationService();
        String pic1 = WebUtils.uploadFile(request, "pic1", WebUtils.SAVE_PATH);
        String pic2 = WebUtils.uploadFile(request, "pic2", WebUtils.SAVE_PATH);
        String pic3 = WebUtils.uploadFile(request, "pic3", WebUtils.SAVE_PATH);
        String pic4 = WebUtils.uploadFile(request, "pic4", WebUtils.SAVE_PATH);
        System.out.println(pic1);
        System.out.println(pic2);
        System.out.println(pic3);
        System.out.println(pic4);
        for (String s : parameterMap.keySet()) {
            for (int i = 0; i < parameterMap.get(s).length; i++) {
                System.out.println(s+":"+parameterMap.get(s)[i]);
            }
        }



       // 转化为实体类
        CatPost catPost = WebUtils.getBean(CatPost.class, request);

    int i = (int) request.getSession().getAttribute("postNum");
        //重新设置文件路径
        catPost.setPic1(pic1);
        catPost.setPic2(pic2);
        catPost.setPic3(pic3);
        catPost.setPic4(pic4);
        catPost.setState("F");
       String username=  request.getParameter("username");
       String exit = request.getParameter("exit");
       if("exit".equals(exit)){

           request.getSession().setAttribute("msg", "修改成功");
           request.getSession().setAttribute("booleans", "true");

           request.getRequestDispatcher("jsp/hs/personal.jsp").forward(request, response);

       }
       // catPost.setUsername(username);
        System.out.println("username:"+username);
        if (service.queryNumber(username)<=0){
            request.getSession().setAttribute("msg", "剩余发帖量不足");
            request.getSession().setAttribute("booleans","true");
            request.getRequestDispatcher("jsp/hs/personal.jsp").forward(request,response);
        }else {
            boolean result = service.addUser(catPost);
            if (result) {
                service.cut_number(username);
                HeadService headService = new HeadService();
                i = headService.queryUserpost(username);
            }

            UserService_lqj userService_lqj = new UserService_lqj();
            System.out.println(result);
            User user = userService_lqj.queryUserByUsername(username);
            request.getSession().setAttribute("postNum", i);
            request.getSession().setAttribute("user", user);
            request.getSession().setAttribute("msg", result ? "发布成功" : "发布失败");
            request.getSession().setAttribute("booleans", "true");

            request.getRequestDispatcher("jsp/hs/personal.jsp").forward(request, response);


        }
    }


}
